<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 4] 4.5 Multiplicative Operators</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:10:11 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_04.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 4<br>Expressions</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch04_06.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-5">4.5 Multiplicative Operators</A></h2>

<P CLASS=para>
<A NAME="CH04.MULT2"></A><A NAME="CH04.MULT3"></A>The multiplicative
operators in Java are binary operators that are used for multiplication
(*), division (<tt CLASS=literal>/</tt>), and the
remainder operation (<tt CLASS=literal>%</tt>). The multiplicative operators
appear in multiplicative expressions:


<p>
<img align=middle src="./figs/jlr0415.gif" alt="[Graphic: Figure from the text]" width=424 height=124 border=0>

<P CLASS=para>
The multiplicative
operators are equal in precedence and are evaluated from left to
right.

<P CLASS=para>
<b>References</b>
<A HREF="ch04_04.htm#JLR2-CH-4-SECT-4">Unary Operators</A>;
<A HREF="ch04_14.htm#JLR2-CH-4-SECT-14">Order of Operations</A>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-5.1">Multiplication Operator *</A></h3>

<P CLASS=para>
<A NAME="CH04.MULT20"></A><A NAME="CH04.MULT22"></A>The binary multiplication operator *
produces a pure value that is the product of its operands.
The *
operator may appear in a multiplicative expression. The multiplication operator
never throws an exception.

<P CLASS=para>
Here is an example that uses the multiplication operator:

<DIV CLASS=screen>
<P>
<PRE>
int doubleIt(int x) {
    return x*2;
}
</PRE>
</DIV>

<P CLASS=para>
The types of both operands of the multiplication operator
must be arithmetic types, or a compile-time error occurs. The *
operator may perform type conversions on its operands:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If
either operand is of type <tt CLASS=literal>double</tt>, the other operand
is converted to <tt CLASS=literal>double</tt> and the operation produces
a <tt CLASS=literal>double</tt> value.

<P>
<li CLASS=listitem>Otherwise, if either
operand is of type <tt CLASS=literal>float</tt>, the other operand is
converted to <tt CLASS=literal>float</tt> and the operation produces
a <tt CLASS=literal>float</tt> value.

<P>
<li CLASS=listitem>Otherwise, if either
operand is of type <tt CLASS=literal>long</tt>, the other operand is
converted to <tt CLASS=literal>long</tt> and the operation produces
a <tt CLASS=literal>long</tt> value.

<P>
<li CLASS=listitem>Otherwise, both operands
are converted to <tt CLASS=literal>int</tt> and the operation produces
an <tt CLASS=literal>int</tt> value.

<P>
</UL>
<P CLASS=para>
If the multiplication
of integer data overflows, the low order bits of the product are returned;
no exception is thrown. The most significant bit of the low order
bits is treated as a sign bit. When overflow occurs, the sign of
the number produced may not be the same as the sign of the mathematically
correct product, due to the limitations of the two's complement
representation used for integer data.

<P CLASS=para>
The multiplication
of floating-point data is governed by the following rules:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If
either operand is not-a-number (NaN), the product is NaN.

<P>
<li CLASS=listitem>If
neither operand is NaN and if both operands have the same sign,
the product is positive.

<P>
<li CLASS=listitem>If neither operand is NaN and
if the operands have different signs, the product is negative.

<P>
<li CLASS=listitem>If one of the operands is positive or negative infinity and
the other operand is positive or negative zero, the product is NaN.

<P>
<li CLASS=listitem>If one of the operands is an infinity value and the other
operand is neither zero nor NaN, the product is either positive
or negative infinity, as determined by the rules governing the sign
of products.

<P>
<li CLASS=listitem>If neither operand is a zero value, an
infinity value, or NaN, the product is rounded to the nearest representable
value. If the magnitude of the product is too large to be represented,
the operation overflows and an infinity value of the appropriate
sign is produced. If the magnitude of the product is too small to
be represented, the operation underflows and a zero value of the
appropriate sign is produced.

<P>
</UL>
<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-5.2">Division Operator /</A></h3>

<P CLASS=para>
<A NAME="CH04.DIV1"></A><A NAME="CH04.DIV2"></A><A NAME="CH04.DIV3"></A>The binary division operator <tt CLASS=literal>/</tt> produces
a pure value that is the quotient of its operands. The left operand
is the dividend and the right operand is the divisor. The <tt CLASS=literal>/</tt>
operator may appear in a multiplicative expression.

<P CLASS=para>
Here
is an example that uses the division operator:

<DIV CLASS=screen>
<P>
<PRE>
int halfIt(int x) {
    return x/2;
}
</PRE>
</DIV>

<P CLASS=para>
The types of both operands of the division operator must be
arithmetic types, or a compile-time error occurs. The <tt CLASS=literal>/</tt>
operator may perform type conversions on its operands:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If
either operand is of type <tt CLASS=literal>double</tt>, the other operand
is converted to <tt CLASS=literal>double</tt> and the operation produces
a <tt CLASS=literal>double</tt> value.

<P>
<li CLASS=listitem>Otherwise, if either
operand is of type <tt CLASS=literal>float</tt>, the other operand is
converted to <tt CLASS=literal>float</tt> and the operation produces
a <tt CLASS=literal>float</tt> value.

<P>
<li CLASS=listitem>Otherwise, if either
operand is of type <tt CLASS=literal>long</tt>, the other operand is
converted to <tt CLASS=literal>long</tt> and the operation produces
a <tt CLASS=literal>long</tt> value.

<P>
<li CLASS=listitem>Otherwise, both operands
are converted to <tt CLASS=literal>int</tt> and the operation produces
an <tt CLASS=literal>int</tt> value.

<P>
</UL>
<P CLASS=para>
The division of integer
data rounds toward zero. If the divisor of an integer division operator
is zero, an <tt CLASS=literal>ArithmeticException</tt> is thrown. If
the dividend is <tt CLASS=literal>Integer.MIN_VALUE</tt> or <tt CLASS=literal>Long.MIN_VALUE</tt>
and the divisor is -1, the quotient produced is <tt CLASS=literal>Integer.MIN_VALUE</tt>
or <tt CLASS=literal>Long.MIN_VALUE</tt>, due to the limitations of
the two's complement representation used for integer data.

<P CLASS=para>
The
division of floating-point data is governed by the following rules:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand is not-a-number (NaN), the quotient is NaN.

<P>
<li CLASS=listitem>If neither operand is NaN and if both operands have the same
sign, the quotient is positive.

<P>
<li CLASS=listitem>If neither operand is
NaN and if the operands have different signs, the quotient is negative.

<P>
<li CLASS=listitem>If both of the operands are positive or negative infinity,
the quotient is NaN.

<P>
<li CLASS=listitem>If the dividend is an infinity
value and the divisor is a finite number, the quotient is either
positive or negative infinity, as determined by the rules governing
the sign of quotients.

<P>
<li CLASS=listitem>If the dividend is a finite number
and the divisor is an infinity value, the quotient is either positive
or negative zero, as determined by the rules governing the sign
of quotients.

<P>
<li CLASS=listitem>If the divisor is positive or negative
zero and the dividend is not zero or NaN, the quotient is either
positive or negative infinity, as determined by the rules governing
the sign of quotients.

<P>
<li CLASS=listitem>If both operands are zero values,
the quotient is NaN.

<P>
<li CLASS=listitem>If the dividend is a zero value
and the divisor is a non-zero finite number, the quotient is either
positive or negative zero, as determined by the rules governing
the sign of quotients.

<P>
<li CLASS=listitem>If the dividend is a non-zero
finite number and the divisor is a zero value, the quotient is either
positive or negative infinity, as determined by the rules governing
the sign of quotients.

<P>
<li CLASS=listitem>If neither operand is a zero
value, an infinity value, or NaN, the quotient is rounded to the
nearest representable value. If the magnitude of the quotient is
too large to be represented, the operation overflows and an infinity
value of the appropriate sign is produced. If the magnitude of the
quotient is too small to be represented, the operation underflows
and a zero value of the appropriate sign is produced.

<P>
</UL>
<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>;
<A HREF="ch10_10.htm">Integer</A>;
<A HREF="ch10_11.htm">Long</A>;
<A HREF="ch09_04.htm#JLR2-CH-9-SECT-4.1.1">Runtime exceptions</A>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-4-SECT-5.3">Remainder Operator %</A></h3>

<P CLASS=para>
<A NAME="CH04.REMAIN1"></A><A NAME="CH04.REMAIN3"></A><A NAME="CH04.REMAIN2"></A>The binary remainder operator <tt CLASS=literal>%</tt> produces
a pure value that is the remainder from an implied division of its
operands. The left operand is the dividend and the right operand
is the divisor. The <tt CLASS=literal>%</tt> operator may appear in
a multiplicative expression.

<P CLASS=para>
Here is an example that
uses the remainder operator:

<DIV CLASS=screen>
<P>
<PRE>
// format seconds into hours, minutes and seconds
String formatTime(int t) {
    int minutes, seconds;
    seconds = t%60;
    t /= 60;
    minutes = t%60;
    return t/60 + ":" + minutes + ":" + seconds;
}
</PRE>
</DIV>

<P CLASS=para>
The types of
both operands of the remainder operator must be arithmetic types,
or a compile-time error occurs. The <tt CLASS=literal>%</tt> operator
may perform type conversions on its operands:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either
operand is of type <tt CLASS=literal>double</tt>, the other operand
is converted to <tt CLASS=literal>double</tt> and the operation produces
a <tt CLASS=literal>double</tt> value.

<P>
<li CLASS=listitem>Otherwise, if either
operand is of type <tt CLASS=literal>float</tt>, the other operand is
converted to <tt CLASS=literal>float</tt> and the operation produces
a <tt CLASS=literal>float</tt> value.

<P>
<li CLASS=listitem>Otherwise, if either
operand is of type <tt CLASS=literal>long</tt>, the other operand is
converted to <tt CLASS=literal>long</tt> and the operation produces
a <tt CLASS=literal>long</tt> value.

<P>
<li CLASS=listitem>Otherwise, both operands
are converted to <tt CLASS=literal>int</tt> and the operation produces
an <tt CLASS=literal>int</tt> value.

<P>
</UL>
<P CLASS=para>
When the remainder
operation is performed on integer data, the following expression
is guaranteed to produce the same value as <tt CLASS=literal>a%b</tt>:

<DIV CLASS=screen>
<P>
<PRE>
a-((a/b)*b)
</PRE>
</DIV>

<P CLASS=para>
The sign of the
value produced by the remainder operator is always the sign of the
dividend. The magnitude of the value 
produced by the remainder operator is always
less than the absolute value of the divisor. 
If the divisor is zero, an <tt CLASS=literal>ArithmeticException</tt>
is thrown.

<P CLASS=para>
Unlike C/C++, Java provides a remainder operation
for floating-point data. The remainder of floating-point data is
computed in a manner similar to the remainder of integer data.
The remainder operation uses a truncating division to compute its
value. This is unlike the IEEE 754 remainder operation, which uses
a rounding division. The IEEE remainder operation is provided by
the <tt CLASS=literal>Math.IEEEremainder()</tt> method.

<P CLASS=para>
The
computation of the remainder of <tt CLASS=literal>double</tt> and <tt CLASS=literal>float</tt>
data is governed by the following rules:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If either operand
is not-a-number (NaN), the remainder is NaN.

<P>
<li CLASS=listitem>If neither
operand is NaN, the sign of the remainder is the same as the sign
of the dividend.

<P>
<li CLASS=listitem>If the dividend is positive or negative
infinity or the divisor is positive or negative zero, the remainder
is NaN.

<P>
<li CLASS=listitem>If the dividend is a finite number and the divisor
is an infinity value, the remainder is equal to the dividend.

<P>
<li CLASS=listitem>If the dividend is a zero value and the divisor is a finite
number, the remainder is equal to the dividend.

<P>
<li CLASS=listitem>If neither
operand is a zero value, an infinity value, or NaN, the remainder
is computed according to the following mathematical formula:

<P CLASS=blockquote><BLOCKQUOTE><P>
<P CLASS=para>
<img src='figs/eq4_1.gif' border=0>
</BLOCKQUOTE><P>
<P CLASS=para>
<tt CLASS=literal>p</tt> is the dividend and <tt CLASS=literal>d</tt>
is the divisor. The notation <img src='figs/floorx.gif' border=0>
means the greatest integer less than or equal to
<I CLASS=emphasis>x</I>&nbsp;; this is called the floor operation.

<P>
</UL>
<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>;
<A HREF="ch10_12.htm">Math</A>;
<A HREF="ch09_04.htm#JLR2-CH-9-SECT-4.1.1">Runtime exceptions</A>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_04.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch04_06.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Unary Operators</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Additive Operators</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
